//
//  GDTabBar2.m
//  GDWeiboNJ
//
//  Created by zhouguodong on 15/4/3.
//  Copyright (c) 2015年 shuanyuan. All rights reserved.
//

#import "GDTabBar2.h"
#import "GDTabBarButton.h"
#import "UIView+Frame.h"

@interface GDTabBar2()
/**
 *  保存当前选中按钮
 */
@property (nonatomic, weak) GDTabBarButton *currenSelectedBtn;
/**
 *  加号按钮
 */
@property (nonatomic, weak)  UIButton *plusBtn;
// 可变数组
@property (nonatomic,strong) NSMutableArray *btnarray;

@end

@implementation GDTabBar2
// 懒加载
- (NSMutableArray *)btnarray
{
    if (_btnarray == nil) {
        _btnarray = [NSMutableArray array];
    }
    return _btnarray;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self setup];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        [self setup];
    }
    return self;
}

- (void)setup
{
    self.backgroundColor = [UIColor whiteColor];
    
    // 添加加号
    [self addpuls];
    
}
// 添加加号
- (void)addpuls
{
    // 创建一个加号按钮, 添加到当前控件中
    // 1.创建按钮
    UIButton *plusBtn = [[UIButton alloc] init];
    plusBtn.tag = 998;
    // 2.设置背景图片
    [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
    [plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
    // 3.设置图标
    [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
    [plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
    // 4.添加
    [self addSubview:plusBtn];

    
    // 5.绑定成员属性
    self.plusBtn = plusBtn;
}
// 利用系统的item作为模型传递数据来,设置自定义tabbar的item
- (void)addItem:(UITabBarItem *)item
{
    // 1.创建选项卡按钮
    GDTabBarButton *btn = [[GDTabBarButton alloc]init];
    
    // 2.设置按钮属性
    [btn setTitle:item.title forState:UIControlStateNormal];
    [btn setImage:item.image forState:UIControlStateNormal];
    [btn setImage:item.selectedImage forState:UIControlStateSelected];
    
    // 3.添加监听事件
    [btn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchDown];
    
    // 3.添加按钮到当前view
    [self addSubview:btn];
    
    
    // 加入数组
    btn.tag = self.btnarray.count;
    [self.btnarray addObject:btn];
    
    // 4.设置默认选中按钮
    if(self.btnarray.count == 1){
        [self btnOnClick:btn];
    }
    // 4.设置默认选中按钮
    // 默认情况下subviews.count等于0, 只要添加了一个按钮那么就代表肯定是第一个
    // 由于添加了加号,所以这里的首页按钮将不是第一个控件
//    if (self.subviews.count == 1) {
//        [self btnOnClick:btn];
//    }
    
}
// 按钮点击的方法
- (void)btnOnClick:(GDTabBarButton *)btn
{
 
    // 判断搭理是否响应代理方法,切换控制器
    if([self.delegate respondsToSelector:@selector(tabBar:selectedBtnfrom:to:)]){
        [self.delegate tabBar:self selectedBtnfrom:self.currenSelectedBtn.tag to:btn.tag];
    }
    // 1.切换按钮的状态
    // 取消上一次选中
    self.currenSelectedBtn.selected = NO;
    // 选中当前点击的按钮
    btn.selected = YES;
    // 记录当前选中的按钮
    self.currenSelectedBtn = btn;
    
    // 3.点击后设置动画
    [UIView animateWithDuration:0.2 animations:^{
        // 缩小
        btn.transform = CGAffineTransformMakeScale(0.5, 0.5);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.1 animations:^{
            // 放大
            btn.transform = CGAffineTransformMakeScale(1.5, 1.5);
        } completion:^(BOOL finished) {
        
            [UIView animateWithDuration:0.1 animations:^{
                // 还原
                btn.transform = CGAffineTransformIdentity;
            }];

        }];
    
    }];
    
}
// 布局子控件
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    NSUInteger index = 0;
    for (UIView *child in self.subviews) {
        if ([child isKindOfClass:[UIButton class]]&& child.tag !=998) {
      
            // 2.计算每个按钮的frame
            CGFloat childW = self.frame.size.width / 5;
            CGFloat childH = self.frame.size.height;
            CGFloat childX = index * childW;
            CGFloat childY = 0;
            
            // 判断是否是中间一个按钮
            if (index == 2) {
                index++;
                childX = index * childW;
            }
            index++;
            child.frame = CGRectMake(childX, childY, childW, childH);
        }
        // 设置加号按钮的frame
        self.plusBtn.size = self.plusBtn.currentBackgroundImage.size;
        self.plusBtn.centerX = self.width * 0.5;
        self.plusBtn.centerY = self.height * 0.5;
    }
    
}


@end
